package com.wwl.cms.utils.designModeTest;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/31 21:00
 * @description:依赖关系传递的三种方式
 */
public class DependencyPass {
    public static void main(String[] args) {
        ChangHong changHong = new ChangHong();
        /*OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.open(changHong);*/
        //通过构造器进行依赖传递
     /*   OpenAndClose openAndClose = new OpenAndClose(changHong);
        openAndClose.open();*/
     //通过setter方法进行依赖传递
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(changHong);
        openAndClose.open();

    }
}
interface ITV{
    public void play();
}
class ChangHong implements ITV{
    public void play(){
        System.out.println("长虹电视机，打开");
    }

}
//方式一：通过接口传递实现依赖
/*interface IOpenAndClose{
    public void open(ITV tv);//抽象方法，接收接口
}


class OpenAndClose implements IOpenAndClose{
    public void open(ITV tv){
        tv.play();
    }
}*/

//方法二通过构造方法依赖传递
/*interface IOpenAndClose{
    public void open();
}



class OpenAndClose implements IOpenAndClose{
    public ITV tv;//成员
    public OpenAndClose(ITV tv){//构造器
        this.tv=tv;
    }
    public void open(){
        this.tv.play();
    }
}*/

//方法三，通过setter方法传递
interface IOpenAndClose{
    public void open();
    public void setTv(ITV tv);
}

class OpenAndClose implements IOpenAndClose{
    private ITV tv;
    public void setTv(ITV tv){
        this.tv=tv;
    }
    public void open(){
        this.tv.play();
    }
}




